﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; 
using VideoNetApplication.Common.Helper;

namespace VideoNetApplication.Common.Dao
{
  public  class StudentDao
    {

        /// <summary>
        /// 判断是否可以注册
        /// </summary>
        public bool IfNameExist(string uName)
        {

            using (EVDBEntities edm=new EVDBEntities())
            {
                var query = edm.V_user.Where(s => s.VU_loginname == uName);
                if(query.Count()>0)
                {
                    return true;
                }
                return false;
            }
        }

      /// <summary>
      /// 验证用户登录
      /// </summary>
      /// <param name="uname"></param>
      /// <param name="pwd"></param>
      /// <param name="schoolID"></param>
      /// <returns></returns>
        public V_user UserLoginBySchoolID(string uname, string pwd, int schoolID)
        {
          using (EVDBEntities edm=new EVDBEntities())
          {
              string newPwd = StringUtils.EncryptPassWord(pwd, StringUtils.PassFormat.MD5);
              var query =
                  edm.V_user.Where(s => s.VU_loginname == uname && s.VU_Password == newPwd && s.SchoolID == schoolID&&s.IsAvailable==true);
              return query.FirstOrDefault();
          }
        
        }
        /// <summary>
        /// 判断用户和邮箱是否是注册的时候一致
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool CheckUnameByEmail(V_user user)
        {

            using (EVDBEntities edm = new EVDBEntities())
            {
                var query = edm.V_user.Where(s => s.VU_loginname == user.VU_loginname && s.VU_email == user.VU_email);
                if(query.Count()>0)
                {
                    return true;

                }
                return false;

            }
        }
      /// <summary>
      /// 添加用户
      /// </summary>
      /// <param name="user"></param>
      /// <returns></returns>
        public int AddUser(V_user user)
        {
            using (EVDBEntities edm=new EVDBEntities())
            {
                var query = edm.V_user.FirstOrDefault(s =>s.VU_loginname==user.VU_loginname);
                if(query==null)
                {
                    query=new V_user();
                    edm.AddToV_user(query);
                    query.VU_loginname = user.VU_loginname;
                    query.create_time = DateTime.Now;

                    query.IsAvailable = user.IsAvailable;
                    query.IsBuyingAccount = user.IsBuyingAccount;

                   
                }
                query.SchoolID = user.SchoolID;
                query.update_time = DateTime.Now;
                query.VU_mobile = user.VU_mobile;
                query.VU_realname = user.VU_realname;
                if (!string.IsNullOrEmpty(user.VU_Password))
                {
                    query.VU_Password = user.VU_Password;
                }
                query.VU_email = user.VU_email;
              if( edm.SaveChanges()>=1)
              {
                  return 1;
              }
                return -1;

            }
        }

        /// <summary>
        /// 根据学员ID修改他自己的密码
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>

        public int UpdateUserPwd(V_user user)
        {

            using (EVDBEntities edm=new EVDBEntities())
            {
                var query = edm.V_user.FirstOrDefault(s => s.VU_loginname == user.VU_loginname);
                if(query!=null)
                {
                    query.update_time = DateTime.Now;
                    query.VU_Password =user.VU_Password;

                }
                if(edm.SaveChanges()>0)
                {
                    return 1;
                }
                return -1;


            }

 
        }

        /// <summary>
        /// 用户登录日志添加 add by yxq
        /// </summary>
        /// <param name="user"></param>
        public int AddUserLog(V_user user)
        {
            using (EVDBEntities edm=new EVDBEntities())
            {
                V_UserLoginLog log = new V_UserLoginLog();
                log.UserID = user.ID;
                log.UserName = user.VU_realname;
                log.SchoolID = user.SchoolID;
                log.Login_Time = DateTime.Now;
                log.Login_IP = HttpContext.Current.Request.UserHostAddress;
                log.Login_Client = HttpContext.Current.Request.Browser.Platform;
                log.Login_Browser = HttpContext.Current.Request.Browser.Browser + HttpContext.Current.Request.Browser.Version;
                log.Logout_Time = DateTime.Now;
                edm.AddToV_UserLoginLog(log);
              return  edm.SaveChanges();
            }

           
            
        }

    }
}
